package com.platform.api.bzproject.service.impl;

import com.baomidou.mybatisplus.core.injector.methods.DeleteBatchByIds;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.platform.api.bzproject.dao.BalanceRecordMapper;
import com.platform.api.bzproject.model.BalanceRecord;
import com.platform.api.bzproject.service.IBalanceRecordService;
import com.platform.common.core.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 余额记录表 服务实现类
 * </p>
 *
 * @author guoshao
 * @since 2021-10-29
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class BalanceRecordServiceImpl extends ServiceImpl<BalanceRecordMapper, BalanceRecord> implements IBalanceRecordService {

    @Autowired
    private BalanceRecordMapper balanceRecordMapper;

    @Override
    public BigDecimal selectSumMoney(BalanceRecord record) {

        return balanceRecordMapper.selectSumMoney(record);
    }

    @Override
    public List<BigDecimal> thisMonth(BalanceRecord record) {
        return balanceRecordMapper.thisMonth(record);
    }

    @Override
    public List<BigDecimal> lastMonth(BalanceRecord record) {
        return balanceRecordMapper.lastMonth(record);
    }

    @Override
    public PageInfo<BalanceRecord> selectBalanceRecordList(BalanceRecord balanceRecord, Integer offset) {

        Integer pageNum = 1;
        if(offset > 0){
            pageNum = (offset / Constant.PAGESIZE) + 1;
        }

        PageHelper.startPage(pageNum, Constant.PAGESIZE);

        PageInfo<BalanceRecord> pageInfo = new PageInfo<>(balanceRecordMapper.selectBalanceRecordList(balanceRecord));
        return pageInfo;
    }

    @Override
    public int getRecordCount(String userId) {
        return balanceRecordMapper.getRecordCount(userId);
    }

    /**
     * 随机获取多少条数据进行删除
     * @param userId
     * @param count
     * @return
     */
    @Override
    public void delRandId(String userId, int count) {
        Map<String,Object> map = new HashMap<>(6);
        map.put("userId",userId);
        map.put("count",count);
        List<Long> idList = balanceRecordMapper.getRandId(map);
        if(idList.size() > 0){
            balanceRecordMapper.deleteBatchIds(idList);
        }

    }

    /**
     * 获取下载数量
     * @param map
     * @return
     */
    @Override
    public Integer getDownCount(Map<String, Object> map) {
        return balanceRecordMapper.getDownCount(map);
    }

    @Override
    public PageInfo<BalanceRecord> selecttBalanceRecordListNew(BalanceRecord balanceRecord, Integer offset) {
        Integer pageNum = 1;
        if(offset > 0){
            pageNum = (offset / Constant.PAGESIZE) + 1;
        }
        PageHelper.startPage(pageNum, Constant.PAGESIZE);
        PageInfo<BalanceRecord> pageInfo = new PageInfo<>(balanceRecordMapper.selecttBalanceRecordListNew(balanceRecord));
        return pageInfo;
    }

}
